package com.wzk.travelspringboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzk.travelspringboot.dao.SystemNoticeDAO;
import com.wzk.travelspringboot.dao.UserDAO;
import com.wzk.travelspringboot.dto.NoticeDTO;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.SystemNotice;
import com.wzk.travelspringboot.entity.User;
import com.wzk.travelspringboot.enumerate.NoticeTypeEnum;
import com.wzk.travelspringboot.service.SystemNoticeService;
import com.wzk.travelspringboot.vo.SystemNoticeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: WangZiKang
 * @Date: 2025/1/14 20:04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SystemNoticeServiceImpl extends ServiceImpl<SystemNoticeDAO, SystemNotice> implements SystemNoticeService {

    private final UserDAO userDAO;

    @Override
    public Result queryAll() {
        //1.查询所有系统公告
        List<SystemNotice> notices = query().orderByDesc("create_time").list();
        //2.判断是否为空
        if (notices.isEmpty()){
            return Result.success("暂无系统公告!", Collections.emptyList());
        }

        ArrayList<SystemNoticeVO> vos = new ArrayList<>();

        //3.封装vo并返回
        for (SystemNotice notice : notices) {
            //转vo
            SystemNoticeVO vo = BeanUtil.copyProperties(notice, SystemNoticeVO.class);
            vo.setType(NoticeTypeEnum.fromCode(notice.getType()));
            //查询用户昵称
            User user = userDAO.selectById(notice.getUserId());
            if (user == null){
                vo.setNickName("系统管理员");
            }else {
                vo.setNickName(user.getNickName());
            }
            vos.add(vo);
        }
        return Result.success("加载成功!",vos);
    }

    @Override
    public Result insertOne(NoticeDTO dto) {
        //1.查询用户是否存在
        User user = userDAO.selectById(dto.getUserId());
        if (user == null || user.getState() != 1){
            return Result.failed("用户不存在或状态异常");
        }
        //2.保存公告到数据库
        SystemNotice systemNotice = new SystemNotice();
        systemNotice.setContent(dto.getContent());
        systemNotice.setType(dto.getType());
        systemNotice.setTitle(dto.getTitle());
        systemNotice.setUserId(dto.getUserId());
        systemNotice.setCreateTime(LocalDateTime.now());
        systemNotice.setUpdateTime(LocalDateTime.now());
        boolean b = save(systemNotice);
        if (!b){
            return Result.failed("公告添加失败，出现未知错误请联系管理员!");
        }
        //3.转换成vo返回
        SystemNoticeVO vo = BeanUtil.copyProperties(systemNotice, SystemNoticeVO.class);
        vo.setNickName(user.getNickName());
        vo.setType(NoticeTypeEnum.fromCode(systemNotice.getType()));
        return Result.success("发布成功!",vo);
    }

    @Override
    public Result deleteById(Long aLong) {
        //1.判断该公告是否存在
        SystemNotice notice = getById(aLong);
        if (notice == null){
            return Result.success("该公告已经删除");
        }
        //2.删除
        boolean b = removeById(aLong);
        if (!b){
            return Result.failed("删除失败");
        }
        return Result.success("删除成功");
    }
}
